package com.zsp.coupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.jsonschema.JsonSerializableSchema;
import com.zsp.coupon.constant.CouponCategory;
import com.zsp.coupon.constant.DistributeTarget;
import com.zsp.coupon.constant.ProdcutLine;
import com.zsp.coupon.convert.CouponCategoryConvert;
import com.zsp.coupon.convert.DistributeTargetConvert;
import com.zsp.coupon.convert.ProductLineConvert;
import com.zsp.coupon.convert.RuleConvert;
import com.zsp.coupon.serialization.CouponTemplateSerilze;
import com.zsp.coupon.vo.TemplateRule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

//优惠卷模板实体类定义：基础属性，规则属性
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "coupon_template")
@JsonSerialize(using = CouponTemplateSerilze.class)
public class CouponTemplate implements Serializable {
    //自增主键
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    private Integer id;


    //是否可用的标志
    @Column(name = "availabe",nullable = false)
    private boolean availabe;

    //是否过期
    @Column(name = "expired",nullable = false)
    private boolean expired;

    //优惠卷名称
    @Column(name = "name",nullable = false)
    private String name;

    //优惠卷logo
    @Column(name = "logo",nullable = false)
    private String logo;

    //优惠卷描述
    @Column(name = "intro",nullable = false)
    private String desc;

    //优惠卷分类
    @Column(name = "category",nullable = false)
    @Convert(converter = CouponCategoryConvert.class)
    private CouponCategory category;

    //产品线
    @Column(name = "product_line",nullable = false)
    @Convert(converter = ProductLineConvert.class)
    private ProdcutLine prodcutLine;


    /** 总数 */
    @Column(name = "coupon_count", nullable = false)
    private Integer count;

    /** 创建时间 */
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    /** 创建用户 */
    @Column(name = "user_id", nullable = false)
    private Long userId;

    /** 优惠券模板的编码 */
    @Column(name = "template_key", nullable = false)
    private String key;

    /** 目标用户 */
    @Column(name = "target", nullable = false)
    @Convert(converter = DistributeTargetConvert.class)
    private DistributeTarget target;

    /** 优惠券规则 */
    @Column(name = "rule", nullable = false)
    @Convert(converter = RuleConvert.class)
    private TemplateRule rule;

    //自定义构造对象
    public CouponTemplate(String name, String logo, String desc, String category,
                          Integer productLine, Integer count, Long userId,
                          Integer target, TemplateRule rule) {
                    this.availabe=false;
                    this.expired=false;
                    this.name=name;
                    this.logo=logo;
                    this.desc=desc;
                    this.category=CouponCategory.of(category);
                    this.prodcutLine=ProdcutLine.of(productLine);
                    this.count=count;
                    this.userId=userId;
                    //优惠卷的唯一编码=4产品线和类型 +8日期+id为扩充的4位
        this.key = productLine.toString() + category +
                new SimpleDateFormat("yyyyMMdd").format(new Date());
        this.target = DistributeTarget.of(target);
        this.rule = rule;

    }
}
